package com.jkt.rms.validator;


import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.jkt.rms.commons.utils.EqualityUtils;
import com.jkt.rms.vo.UsersVO;

public class PasswordValidator implements Validator {
	
	@Override
	public boolean supports(Class clazz) {
		return clazz.equals(UsersVO.class);
		
	}
	
	public void validate(Object obj, Errors errors) {
		UsersVO user = (UsersVO) obj;
		if (user == null) {
			errors.rejectValue("usrid", "error.login.not-specified", null,"Value required.");
			return;
		}
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "currentPassword", "current.password.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "newPassword", "newpassword.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword", "confirmpassword.required");
		if (EqualityUtils.isNotNull(user.getCurrentPassword()) && !EqualityUtils.equals(user.getCurrentPassword(), "admin")) { //TO DO
			errors.rejectValue("currentPassword", "error.currentpassword");
		}
		if (EqualityUtils.isNotNull(user.getNewPassword()) && EqualityUtils.isNotNull(user.getConfirmPassword()) &&
				!EqualityUtils.equals(user.getNewPassword(), user.getConfirmPassword())) {
			errors.rejectValue("confirmPassword", "error.confirmpassword");
		} 
	}
}
